package com.dashu.park.service;

import com.dashu.base.enums.ReplRyCode;
import com.dashu.base.enums.common.GenderType;
import com.dashu.base.enums.common.Status;
import com.dashu.base.enums.device.Iden;
import com.dashu.base.enums.device.PicType;
import com.dashu.base.enums.enterprise.WorkersType;
import com.dashu.base.enums.school.TeacherType;
import com.dashu.base.utils.CopyUtil;
import com.dashu.base.utils.RegularUtils;
import com.dashu.base.utils.UIDUtil;
import com.dashu.base.utils.VerificationUtil;
import com.dashu.base.vo.DevicePicAddVO;
import com.dashu.base.vo.DevicePicDelReqVO;
import com.dashu.base.vo.DevicePicUpdReqVO;
import com.dashu.park.dto.company.EnterpriseDTO;
import com.dashu.park.dto.company.EnterpriseWorkersDTO;
import com.dashu.park.dto.company.EnterpriseWorkersListDTO;
import com.dashu.park.handler.VerificationException;
import com.dashu.park.model.EnterpriseWorkers;
import com.dashu.park.model.QEnterpriseWorkers;
import com.dashu.park.result.PageEnty;
import com.dashu.park.vo.UpdateEntityVO;
import com.dashu.park.vo.company.EnterpriseWorkersListVO;
import com.dashu.park.vo.company.EnterpriseWorkersVO;
import com.dashu.park.vo.company.UpEnterpriseWorkersVO;
import com.google.common.collect.Lists;
import com.querydsl.core.BooleanBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.FlushModeType;
import javax.transaction.Transactional;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class EnterpriseWorkersService extends DataService {

    @Autowired
    private EnterpriseService enterpriseService;
//    @Autowired
//    private CompanyClsService companyClsService;
//    @Autowired
//    private CompanyDepartmentService companyDepartmentService;
//    @Autowired
//    private TeacherClsService teacherClsService;
//    @Autowired
//    private DeviceService deviceService;

    @Transactional
    public EnterpriseWorkersDTO add(EnterpriseWorkersVO enterpriseWorkersVO) {
        EnterpriseWorkers enterpriseWorkers = CopyUtil.copyEnty(enterpriseWorkersVO, EnterpriseWorkers.class);
        boolean isPhone = findByWorkersPhone(enterpriseWorkersVO.getWorkersPhone());
        if (!isPhone)
            throw new VerificationException(ReplRyCode.EXIST_PHONE, ReplRyCode.EXIST_PHONE.getMessage());
        try {
            long userId = Long.parseLong(VerificationUtil.getUserId());
            enterpriseWorkers.setId(UIDUtil.nextId());
            enterpriseWorkers.setCreateUser(userId);
            enterpriseWorkers.setCreateTime(System.currentTimeMillis());
            enterpriseWorkers.setWorkersType(WorkersType.map.get(enterpriseWorkersVO.getWorkersType()));
            if (!StringUtils.isEmpty(enterpriseWorkersVO.getIcon())) {
                enterpriseWorkers.setStatus(Status.INITIALIZATION);
            } else {
                enterpriseWorkers.setStatus(Status.UNINITIALIZED);
            }
            enterpriseWorkers.setDepartmentId(enterpriseWorkersVO.getDepartmentId());
            entityManager.persist(enterpriseWorkers);
//            entityManager.flush();
            entityManager.setFlushMode(FlushModeType.AUTO);

        } catch (Exception e) {
            e.printStackTrace();
            throw new VerificationException(ReplRyCode.DATA_NOT_INVALID, ReplRyCode.DATA_NOT_INVALID.getMessage());
        }
        EnterpriseWorkersDTO enterpriseWorkersDTO = CopyUtil.copyEnty(enterpriseWorkers, EnterpriseWorkersDTO.class);
        return enterpriseWorkersDTO;

    }

//    @Transactional
//    public List<EnterpriseWorkersDTO> batchAdd(List<ExcelTeacher> excelTeachers, long companyId) {
//        EnterpriseWorkersVO enterpriseWorkersVO = new EnterpriseWorkersVO();
//        enterpriseWorkersVO.setCompanyId(companyId);
//        List<EnterpriseWorkersDTO> enterpriseWorkersDTO = Lists.newArrayList();
//        excelTeachers.stream().forEach(et -> {
//            enterpriseWorkersVO.setTeacherName(et.getName());
//            enterpriseWorkersVO.setGender(et.getGender().name());
//            if (et.getPhone().length() != 11) {
//                throw new VerificationException(ReplRyCode.ERROR_PHONE, ReplRyCode.ERROR_PHONE.getMessage());
//            }
//            enterpriseWorkersVO.setTeacherPhone(Long.parseLong(et.getPhone()));
//            enterpriseWorkersDTO.add(add(enterpriseWorkersVO));
//        });
//
//        return enterpriseWorkersDTO;
//
//    }

    @Transactional
    public boolean deleteByEnterpriseWorkersId(long enterpriseWorkersId) {
        QEnterpriseWorkers qEnterpriseWorkers = QEnterpriseWorkers.enterpriseWorkers;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qEnterpriseWorkers.id.eq(enterpriseWorkersId));
        EnterpriseWorkers EnterpriseWorkers;
        queryFactory.select();
        EnterpriseWorkers = queryFactory
                .select(qEnterpriseWorkers)
                .from(qEnterpriseWorkers)
                .where(booleanBuilder.getValue()).fetchOne();
        if (EnterpriseWorkers == null) return false;
        deleteImgTeacher(EnterpriseWorkers.getId());
        entityManager.remove(EnterpriseWorkers);
//            entityManager.flush();
        entityManager.setFlushMode(FlushModeType.AUTO);
        return true;

    }


    @Transactional
    public boolean deleteBatchTeacher(List<Long> enterpriseWorkersIds) {
        enterpriseWorkersIds.stream().forEach(ct -> {
            if (!deleteByEnterpriseWorkersId(ct))
                throw new VerificationException(ReplRyCode.BATCH_REMOV_ERROR, ReplRyCode.BATCH_REMOV_ERROR.getMessage());
        });
        return true;

    }


    @Transactional
    public boolean deleteImgTeacher(long enterpriseWorkersId) {
        QEnterpriseWorkers qEnterpriseWorkers = QEnterpriseWorkers.enterpriseWorkers;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qEnterpriseWorkers.id.eq(enterpriseWorkersId));
        EnterpriseWorkers enterpriseWorkers =
                queryFactory
                        .select(qEnterpriseWorkers)
                        .from(qEnterpriseWorkers)
                        .where(booleanBuilder.getValue())
                        .fetchOne();
        DevicePicDelReqVO devicePicDelReqVO = new DevicePicDelReqVO();
        devicePicDelReqVO.setUserId(String.valueOf(enterpriseWorkers.getId()));
        devicePicDelReqVO.setUserName(enterpriseWorkers.getWorkersName());
        devicePicDelReqVO.setIden(Iden.TEACHER);
        devicePicDelReqVO.setResp(enterpriseWorkers.getIcon());
//        deviceService.DeldeviceBaseHand(enterpriseWorkers.getCompanyId(), devicePicDelReqVO);

        return true;

    }

    @Transactional
    public boolean updateImgTeacher(long enterpriseWorkersId) {
        QEnterpriseWorkers qEnterpriseWorkers = QEnterpriseWorkers.enterpriseWorkers;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qEnterpriseWorkers.id.eq(enterpriseWorkersId));
        EnterpriseWorkers enterpriseWorkers =
                queryFactory
                        .select(qEnterpriseWorkers)
                        .from(qEnterpriseWorkers)
                        .where(booleanBuilder.getValue())
                        .fetchOne();
        DevicePicUpdReqVO devicePicUpdReqVO = new DevicePicUpdReqVO();
        devicePicUpdReqVO.setUserId(String.valueOf(enterpriseWorkers.getId()));
        devicePicUpdReqVO.setUserName(enterpriseWorkers.getWorkersName());
        devicePicUpdReqVO.setIden(Iden.TEACHER);
        devicePicUpdReqVO.setPicType(PicType.URL);
        devicePicUpdReqVO.setResp(enterpriseWorkers.getIcon());
//        deviceService.UpdeviceBaseHand(enterpriseWorkers.getCompanyId(), devicePicUpdReqVO);
        return true;

    }

    @Transactional
    public boolean issueBatchTeacher(List<Long> enterpriseWorkersIds) {
        QEnterpriseWorkers qEnterpriseWorkers = QEnterpriseWorkers.enterpriseWorkers;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qEnterpriseWorkers.id.in(enterpriseWorkersIds));
        List<EnterpriseWorkers> enterpriseWorkerss =
                queryFactory
                        .select(qEnterpriseWorkers)
                        .from(qEnterpriseWorkers)
                        .where(booleanBuilder.getValue())
                        .fetch();
        enterpriseWorkerss.stream().forEach(ct -> {
            DevicePicAddVO devicePicAddVO = new DevicePicAddVO();
            devicePicAddVO.setUserId(Iden.TEACHER + "_" + String.valueOf(ct.getId()));
            devicePicAddVO.setUserName(ct.getWorkersName());
            devicePicAddVO.setIden(Iden.TEACHER);
            devicePicAddVO.setPicType(PicType.URL);
            devicePicAddVO.setResp(ct.getIcon());
//            deviceService.AdddeviceBaseHand(ct.getCompanyId(), devicePicAddVO);
        });

        return true;

    }

    @Transactional
    public EnterpriseWorkersDTO updataByEnterpriseWorkersId(UpdateEntityVO<UpEnterpriseWorkersVO> updateEntityVO) throws Exception {
        QEnterpriseWorkers qEnterpriseWorkers = QEnterpriseWorkers.enterpriseWorkers;
        long userId = Long.parseLong(VerificationUtil.getUserId());
        UpEnterpriseWorkersVO upEnterpriseWorkersVO = updateEntityVO.getEntityVO();
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qEnterpriseWorkers.id.eq(upEnterpriseWorkersVO.getId()));
        EnterpriseWorkers enterpriseWorkers =
                queryFactory
                        .select(qEnterpriseWorkers)
                        .from(qEnterpriseWorkers)
                        .where(booleanBuilder.getValue()).fetchOne();
        if (enterpriseWorkers == null)
            throw new VerificationException(ReplRyCode.COMPANY_TEACHER_NOT_DATA, ReplRyCode.COMPANY_TEACHER_NOT_DATA.getMessage());
        enterpriseWorkers.setUpdateTime(new Date().getTime());
        enterpriseWorkers.setUpdateUser(userId);
        if (upEnterpriseWorkersVO.getWorkersPhone() != 0)
            enterpriseWorkers.setWorkersPhone(upEnterpriseWorkersVO.getWorkersPhone());
        if (!StringUtils.isEmpty(upEnterpriseWorkersVO.getWorkersName()))
            enterpriseWorkers.setWorkersName(upEnterpriseWorkersVO.getWorkersName());
        if (upEnterpriseWorkersVO.getDepartmentId() != 0)
            enterpriseWorkers.setDepartmentId(upEnterpriseWorkersVO.getDepartmentId());
        if (!StringUtils.isEmpty(upEnterpriseWorkersVO.getIcon())) {
            enterpriseWorkers.setIcon(upEnterpriseWorkersVO.getIcon());
            enterpriseWorkers.setStatus(Status.INITIALIZATION);
            updateImgTeacher(enterpriseWorkers.getId());
        }
        enterpriseWorkers = entityManager.merge(enterpriseWorkers);
//        entityManager.flush();
        entityManager.setFlushMode(FlushModeType.AUTO);
        EnterpriseWorkersDTO EnterpriseWorkersDTO = CopyUtil.copyEnty(enterpriseWorkers, EnterpriseWorkersDTO.class);
        return EnterpriseWorkersDTO;

    }


//    @Transactional
//    public void updataAttendanceCompany(long companyId, long attendanceId, TeacherType teacherTypes) throws Exception {
//        QEnterpriseWorkers qEnterpriseWorkers = QEnterpriseWorkers.enterpriseWorkers;
//        long userId = Long.parseLong(VerificationUtil.getUserId());
//        BooleanBuilder booleanBuilder = new BooleanBuilder();
//        booleanBuilder.and(qEnterpriseWorkers.companyId.eq(companyId));
//        booleanBuilder.and(qEnterpriseWorkers.teacherType.in(teacherTypes));
//        List<EnterpriseWorkers> enterpriseWorkerss =
//                queryFactory
//                        .select(qEnterpriseWorkers)
//                        .from(qEnterpriseWorkers)
//                        .where(booleanBuilder.getValue())
//                        .fetch();
//        enterpriseWorkerss.stream().forEach(ct -> {
//            ct.setUpdateUser(userId);
//            ct.setUpdateTime(new Date().getTime());
//            ct.setAttendanceId(attendanceId);
//            entityManager.merge(ct);
//            //entityManager.flush();
//            entityManager.setFlushMode(FlushModeType.AUTO);
//        });
//    }
//
//    @Transactional
//    public void updataGateCompany(long companyId, long gateId, TeacherType teacherTypes) throws Exception {
//        QEnterpriseWorkers qEnterpriseWorkers = QEnterpriseWorkers.enterpriseWorkers;
//        long userId = Long.parseLong(VerificationUtil.getUserId());
//        BooleanBuilder booleanBuilder = new BooleanBuilder();
//        booleanBuilder.and(qEnterpriseWorkers.companyId.eq(companyId));
//        booleanBuilder.and(qEnterpriseWorkers.teacherType.in(teacherTypes));
//        List<EnterpriseWorkers> enterpriseWorkerss =
//                queryFactory
//                        .select(qEnterpriseWorkers)
//                        .from(qEnterpriseWorkers)
//                        .where(booleanBuilder.getValue())
//                        .fetch();
//        enterpriseWorkerss.stream().forEach(ct -> {
//            ct.setUpdateUser(userId);
//            ct.setUpdateTime(new Date().getTime());
//            ct.setGateId(gateId);
//            entityManager.merge(ct);
//            //entityManager.flush();
//            entityManager.setFlushMode(FlushModeType.AUTO);
//        });
//    }
//
//
//    @Transactional
//    public void updataAttendance(long teacherId, long attendanceId) throws Exception {
//        QEnterpriseWorkers qEnterpriseWorkers = QEnterpriseWorkers.enterpriseWorkers;
//        long userId = Long.parseLong(VerificationUtil.getUserId());
//        BooleanBuilder booleanBuilder = new BooleanBuilder();
//        booleanBuilder.and(qEnterpriseWorkers.id.eq(teacherId));
//        EnterpriseWorkers enterpriseWorkerss =
//                queryFactory
//                        .select(qEnterpriseWorkers)
//                        .from(qEnterpriseWorkers)
//                        .where(booleanBuilder.getValue())
//                        .fetchOne();
//
//        enterpriseWorkerss.setUpdateUser(userId);
//        enterpriseWorkerss.setUpdateTime(new Date().getTime());
//        enterpriseWorkerss.setAttendanceId(attendanceId);
//        entityManager.merge(enterpriseWorkerss);
//        //entityManager.flush();
//        entityManager.setFlushMode(FlushModeType.AUTO);
//    }
//
//    @Transactional
//    public void updataGate(long teacherId, long gateId) throws Exception {
//        QEnterpriseWorkers qEnterpriseWorkers = QEnterpriseWorkers.enterpriseWorkers;
//        long userId = Long.parseLong(VerificationUtil.getUserId());
//        BooleanBuilder booleanBuilder = new BooleanBuilder();
//        booleanBuilder.and(qEnterpriseWorkers.companyId.eq(teacherId));
//        EnterpriseWorkers enterpriseWorkerss =
//                queryFactory
//                        .select(qEnterpriseWorkers)
//                        .from(qEnterpriseWorkers)
//                        .where(booleanBuilder.getValue())
//                        .fetchOne();
//
//        enterpriseWorkerss.setUpdateUser(userId);
//        enterpriseWorkerss.setUpdateTime(new Date().getTime());
//        enterpriseWorkerss.setGateId(gateId);
//        entityManager.merge(enterpriseWorkerss);
//        //entityManager.flush();
//        entityManager.setFlushMode(FlushModeType.AUTO);
//    }
//
//
//    @Transactional
//    public void updataAttendanceDepartment(long department, long attendanceId, List<TeacherType> teacherTypes) throws Exception {
//        List<CompanyCls> teacherCls = companyClsService.findByDepartmentId(department);
//        teacherCls.stream().forEach(tc -> {
////            updataAttendanceCls
//        });
//    }


    public List<EnterpriseWorkersDTO> findByCreteUser(PageEnty<Long> baseVo) {
        QEnterpriseWorkers qEnterpriseWorkers = QEnterpriseWorkers.enterpriseWorkers;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qEnterpriseWorkers.createUser.eq(baseVo.getBaseId()));
        List<EnterpriseWorkers> enterpriseWorkerss = queryFactory
                .select(qEnterpriseWorkers)
                .from(qEnterpriseWorkers)
                .where(booleanBuilder.getValue())
                .fetch();
        List<EnterpriseWorkersDTO> recycleDTOS = CopyUtil.copyList(enterpriseWorkerss, EnterpriseWorkersDTO.class);
        return recycleDTOS;

    }

    public EnterpriseWorkersDTO findByEnterpriseWorkersId(long enterpriseWorkersId) {
        QEnterpriseWorkers qEnterpriseWorkers = QEnterpriseWorkers.enterpriseWorkers;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qEnterpriseWorkers.id.eq(enterpriseWorkersId));
        EnterpriseWorkers enterpriseWorkers = queryFactory
                .select(qEnterpriseWorkers)
                .from(qEnterpriseWorkers)
                .where(booleanBuilder.getValue()).fetchOne();
        if (enterpriseWorkers == null)
            throw new VerificationException(ReplRyCode.COMPANY_TEACHER_NOT_DATA, ReplRyCode.COMPANY_TEACHER_NOT_DATA.getMessage());
        EnterpriseWorkersDTO EnterpriseWorkersDTO = CopyUtil.copyEnty(enterpriseWorkers, EnterpriseWorkersDTO.class);
        return EnterpriseWorkersDTO;
    }

    public boolean findByWorkersPhone(long workersPhone) {
        QEnterpriseWorkers qEnterpriseWorkers = QEnterpriseWorkers.enterpriseWorkers;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qEnterpriseWorkers.workersPhone.eq(workersPhone));
        EnterpriseWorkers enterpriseWorkers = queryFactory
                .select(qEnterpriseWorkers)
                .from(qEnterpriseWorkers)
                .where(booleanBuilder.getValue()).fetchOne();
        if (enterpriseWorkers == null)
            return true;
        return false;
    }


//    public List<EnterpriseWorkers> findByUserId(long userId) {
//        QEnterpriseWorkers qEnterpriseWorkers = QEnterpriseWorkers.enterpriseWorkers;
//        List<Long> companieIds = Lists.newArrayList();
//        List<Company> companies = companyService.findByBaseId(userId);
//        companies.stream().forEach(c -> {
//            companieIds.add(c.getId());
//        });
//        BooleanBuilder booleanBuilder = new BooleanBuilder();
//        booleanBuilder.and(qEnterpriseWorkers.companyId.in(companieIds));
//        List<EnterpriseWorkers> enterpriseWorkerss = queryFactory
//                .select(qEnterpriseWorkers)
//                .from(qEnterpriseWorkers)
//                .where(booleanBuilder.getValue())
//                .fetch();
//        return enterpriseWorkerss;
//
//    }


//    public void deleteCompany(long companyId) {
//        List<EnterpriseWorkers> enterpriseWorkerss = findByCompanieId(companyId);
//        enterpriseWorkerss.stream().forEach(ct -> {
//            ct.setCompanyId(0);
//            entityManager.merge(ct);
////        entityManager.flush();
//            entityManager.setFlushMode(FlushModeType.AUTO);
//        });
//    }
//
//
//    public List<EnterpriseWorkers> findByCompanieId(long companyId) {
//        QEnterpriseWorkers qEnterpriseWorkers = QEnterpriseWorkers.enterpriseWorkers;
//        BooleanBuilder booleanBuilder = new BooleanBuilder();
//        booleanBuilder.and(qEnterpriseWorkers.companyId.eq(companyId));
//        List<EnterpriseWorkers> enterpriseWorkerss = queryFactory
//                .select(qEnterpriseWorkers)
//                .from(qEnterpriseWorkers)
//                .where(booleanBuilder.getValue())
//                .fetch();
//        return enterpriseWorkerss;
//
//    }
//
//    public long countCompanyId(long companyId) throws Exception {
//        QEnterpriseWorkers qEnterpriseWorkers = QEnterpriseWorkers.enterpriseWorkers;
//        BooleanBuilder booleanBuilder = new BooleanBuilder();
//        booleanBuilder.and(qEnterpriseWorkers.companyId.eq(companyId));
//        long count = queryFactory
//                .select(qEnterpriseWorkers)
//                .from(qEnterpriseWorkers)
//                .where(booleanBuilder.getValue())
//                .fetchCount();
//        return count;
//    }
//
//
//    public List<EnterpriseWorkers> findByCompanyDepartmentId(long companieDepartmentId) {
//        List<EnterpriseWorkers> enterpriseWorkersAll = Lists.newArrayList();
//        List<CompanyCls> companyCls = companyClsService.findByDepartmentId(companieDepartmentId);
//        companyCls.stream().forEach(cc -> {
//            List<EnterpriseWorkers> enterpriseWorkerss = findByCompanieClsId(cc.getId());
//            enterpriseWorkersAll.addAll(enterpriseWorkerss);
//        });
//        enterpriseWorkersAll.sort(Comparator.comparing(EnterpriseWorkers -> EnterpriseWorkers.getId()));
//        return enterpriseWorkersAll;
//
//    }


    public List<EnterpriseWorkers> screen(EnterpriseWorkersListVO enterpriseWorkersVO) throws Exception {
//        long userId = Long.parseLong(VerificationUtil.getUserId());
//        if (enterpriseWorkersVO.getDepartmentId() != 0) {
//            return findByCompanyDepartmentId(enterpriseWorkersVO.getCompanyDepartmentId());
//        } else if (enterpriseWorkersVO.getCompanyId() != 0) {
//            return findByCompanieId(enterpriseWorkersVO.getCompanyId());
//        }
//        return findByUserId(userId);
        return null;
    }


    public long countWorkersList(PageEnty<EnterpriseWorkersListVO> enterpriseWorkersListVO) throws Exception {
        EnterpriseWorkersListVO enterpriseWorkersVO = enterpriseWorkersListVO.getVo();
//        List<EnterpriseWorkersListDTO> recycleDTOS = Lists.newArrayList();
        List<EnterpriseWorkers> enterpriseWorkerss = screen(enterpriseWorkersVO);
        if (!StringUtils.isEmpty(enterpriseWorkersVO.getGender()))
            enterpriseWorkerss = enterpriseWorkerss.stream().filter(ct -> ct.getGender().equals(GenderType.map.get(enterpriseWorkersVO.getGender()))).collect(Collectors.toList());
        if (!StringUtils.isEmpty(enterpriseWorkersVO.getWorkersType()))
            enterpriseWorkerss = enterpriseWorkerss.stream().filter(ct -> ct.getWorkersType().equals(TeacherType.map.get(enterpriseWorkersVO.getWorkersType()))).collect(Collectors.toList());
        if (!StringUtils.isEmpty(enterpriseWorkersVO.getStatus()))
            enterpriseWorkerss = enterpriseWorkerss.stream().filter(ct -> ct.getStatus().equals(Status.map.get(enterpriseWorkersVO.getStatus()))).collect(Collectors.toList());
        if (!StringUtils.isEmpty(enterpriseWorkersVO.getWorkersName()))
            enterpriseWorkerss = enterpriseWorkerss.stream().filter(ct -> RegularUtils.matchStringByIndexOf(ct.getWorkersName(), enterpriseWorkersVO.getWorkersName())).collect(Collectors.toList());
        int startPage = (enterpriseWorkersListVO.getPage() - 1) * enterpriseWorkersListVO.getNum();
        int endPage = enterpriseWorkersListVO.getPage() * enterpriseWorkersListVO.getNum() > enterpriseWorkerss.size() ? enterpriseWorkerss.size() : enterpriseWorkersListVO.getPage() * enterpriseWorkersListVO.getNum();
        enterpriseWorkerss.subList(startPage, endPage);
        return enterpriseWorkerss.size();
    }


    public List<EnterpriseWorkersListDTO> pageWorkersList(PageEnty<EnterpriseWorkersListVO> enterpriseWorkersListVO) throws
            Exception {
        EnterpriseWorkersListVO enterpriseWorkersVO = enterpriseWorkersListVO.getVo();
        List<EnterpriseWorkersListDTO> recycleDTOS = Lists.newArrayList();
        List<EnterpriseWorkers> enterpriseWorkerss = screen(enterpriseWorkersVO);
        if (!StringUtils.isEmpty(enterpriseWorkersVO.getGender()))
            enterpriseWorkerss = enterpriseWorkerss.stream().filter(ct -> ct.getGender().equals(GenderType.map.get(enterpriseWorkersVO.getGender()))).collect(Collectors.toList());
        if (!StringUtils.isEmpty(enterpriseWorkersVO.getWorkersType()))
            enterpriseWorkerss = enterpriseWorkerss.stream().filter(ct -> ct.getWorkersType().equals(TeacherType.map.get(enterpriseWorkersVO.getWorkersType()))).collect(Collectors.toList());
        if (!StringUtils.isEmpty(enterpriseWorkersVO.getStatus()))
            enterpriseWorkerss = enterpriseWorkerss.stream().filter(ct -> ct.getStatus().equals(Status.map.get(enterpriseWorkersVO.getStatus()))).collect(Collectors.toList());
        if (!StringUtils.isEmpty(enterpriseWorkersVO.getWorkersName()))
            enterpriseWorkerss = enterpriseWorkerss.stream().filter(ct -> RegularUtils.matchStringByIndexOf(ct.getWorkersName(), enterpriseWorkersVO.getWorkersName())).collect(Collectors.toList());
        int startPage = (enterpriseWorkersListVO.getPage() - 1) * enterpriseWorkersListVO.getNum();
        int endPage = enterpriseWorkersListVO.getPage() * enterpriseWorkersListVO.getNum() > enterpriseWorkerss.size() ? enterpriseWorkerss.size() : enterpriseWorkersListVO.getPage() * enterpriseWorkersListVO.getNum();
        enterpriseWorkerss.subList(startPage, endPage);
        enterpriseWorkerss.sort(Comparator.comparing(EnterpriseWorkers::getCreateTime).reversed());
        enterpriseWorkerss.stream().forEach(ct -> {
            EnterpriseWorkersListDTO enterpriseWorkersListDTO = CopyUtil.copyEnty(ct, EnterpriseWorkersListDTO.class);
            enterpriseWorkersListDTO.setIcon(ct.getIcon());
            enterpriseWorkersListDTO.setEnterpriseName(ct.getWorkersName());
            enterpriseWorkersListDTO.setWorkersPhone(ct.getWorkersPhone());
            enterpriseWorkersListDTO.setEnterpriseId(ct.getEnterpriseId());
            EnterpriseDTO enterpriseDTO = enterpriseService.findByEnterpriseId(ct.getEnterpriseId());
            enterpriseWorkersListDTO.setEnterpriseName(enterpriseDTO.getEnterpriseName());
            enterpriseWorkersListDTO.setWorkersType(ct.getWorkersType());
            enterpriseWorkersListDTO.setGender(ct.getGender());
            enterpriseWorkersListDTO.setStatus(ct.getStatus());
            recycleDTOS.add(enterpriseWorkersListDTO);
        });
        return recycleDTOS;

    }

}

